/**
 * fileName: BeanUtils.java
 *     Date: 2012-4-29
 **/
package com.stranger.common.util;


import java.lang.reflect.Method;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * @作者 stranger.lin
 * 
 * @时间 2012-4-29
 * @描述 对象辅助类
 */
public class BeanUtils {

	private static final String context="applicationContext.xml";
	
	public static void copyProperty(Object oldObj, Object newObj) {
		try {
			if (oldObj == null||newObj==null) {
				//throw new Exception("对象为空");
			} else {
				Method[] oldMethods =Class.forName(oldObj.getClass().getName()).getMethods();
				Method[] newMethods = Class.forName(newObj.getClass().getName()).getMethods();
				for (int i = 0; i < oldMethods.length; i++) {
					if (oldMethods[i].getName().startsWith("get")) {
						for (int j = 0; j < newMethods.length; j++) {
							if (newMethods[j].getName().startsWith("get")) {
								if (oldMethods[i].getName().equals(newMethods[j].getName())) {
									Object value=oldMethods[i].invoke(oldObj);
									
									setFiedValue(newObj,oldMethods[i].getName(),value);
										
									
									
								}
							}
							
						}
					}
					

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void setFiedValue(Object newObj,String methodName,Object value)throws Exception{
		methodName=methodName.substring(3);
		Method[] newMethods = newObj.getClass().getMethods();
		for (int i = 0; i < newMethods.length; i++) {
			String _methodName=newMethods[i].getName().substring(3);
			if (newMethods[i].getName().startsWith("set")&&methodName.equalsIgnoreCase(_methodName)) {
				newMethods[i].invoke(newObj, value);
			}
			
			
		}
	}

	
	public static ApplicationContext getApplicationContext(){
		ApplicationContext appplication=new ClassPathXmlApplicationContext(context);
		return appplication;
	}
	
	
	public static void main(String[] args) {
		//UserVO user =null;
		//user.setUserName("11");
		//user.setPassword("222");
		//TeachersBean bean = new TeachersBean();

		//BeanUtils.copyProperty(user, bean);
		//System.err.println(user.getClass().getName());
	}

}
